<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Agp\CoreBundle\Type;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Agp\CoreBundle\Type\Set;
use Doctrine\DBAL\Types\ConversionException;

/**
 * Description of SetType
 *
 * @author bondcs
 * 
 * // A ADIÇÃO DE TYPE ESTA NO BOOT DO AgpAgapeBundle.php
 */
class SetType extends Type
{
    public function getName()
    {
        return 'set';
    }
   
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
       return $platform->getIntegerTypeDeclarationSQL($fieldDeclaration);
    }
 
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        $value = new Set($value);
        if ($value == null) {
            return null;
        } else if ($value instanceof Set) {
            return $value->toInteger();
        } else {
            $r = new \ReflectionClass('Set');
            throw new \InvalidArgumentException(sprintf('$value must be instance of %s.', $r->getNamespace()));
        }
    }
 
    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        if ($value === null) {
            return null;
        }
 
        try {
            $val = new Set($value);
        }catch (\Exception $e) {
            throw ConversionException::conversionFailed($value, $this->getName());
        }
    
        return $val->toArray();
    }
   
    public function getBindingType()
    {
        return \PDO::PARAM_INT;
    }
}

?>
